﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;

namespace LanguageConfigurationSolution
{
    public class ManageLanguageConfiguration : Microsoft.SharePoint.WebControls.LayoutsPageBase
    {
        protected TextBox tbDefaultList;
        protected DropDownList DdlDefaultSubWeb;
        protected Label lblStatus;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!IsPostBack)
            {
                SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    SPSite site = new SPSite(SPContext.Current.Site.ID);
                    string defaultsubweb = site.RootWeb.Properties[ RootLandingPage.DEFAULTSUBWEBPROPERTYNAME ];
                    foreach (SPWeb web in site.RootWeb.Webs)
                    {
                        ListItem li = new ListItem(web.Name,web.Name);
                        if (defaultsubweb != null && string.Compare(web.Name, defaultsubweb, true) == 0)
                        {
                            li.Selected = true;
                        }
                        DdlDefaultSubWeb.Items.Add(li);
                    }                    
                }
                );
            }
        }

        public void SaveClick(object sender, EventArgs e)
        {
            if (SPContext.Current != null)
            {
                using (SPWeb rootweb = SPContext.Current.Site.OpenWeb(SPContext.Current.Site.RootWeb.ID))
                {
                    rootweb.Properties["LanguageConfigurationList"] = tbDefaultList.Text;
                    rootweb.Properties[RootLandingPage.DEFAULTSUBWEBPROPERTYNAME] = DdlDefaultSubWeb.SelectedValue;
                    rootweb.Properties.Update();
                }
                lblStatus.Text = "Saved...";
            }

        }
    }
}
